package com.zyz.bookborrowing.servlet;

import com.zyz.bookborrowing.bean.Book;
import com.zyz.bookborrowing.bean.PageBean;
import com.zyz.bookborrowing.service.BookServiceImpl;
import com.zyz.bookborrowing.service.IBookService;
import com.zyz.bookborrowing.utils.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "bookServlet", urlPatterns = "/bookServlet")

public class BookServlet extends HttpServlet {

    private IBookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
//        拦截器 CharacterEncodingFilter 已完成了配置
        String type = req.getParameter(Constant.REQUEST_PARAMETER_TYPE);
        if (Constant.SERVLET_TYPE_QUERY.equals(type)){
            queryBookList(req, resp);
        } else if (Constant.SERVLET_TYPE_SAVE.equals(type)){
//            完成Book数据的添加
            saveBook(req, resp);
        } else if (Constant.SERVLET_TYPE_QUERYBYID.equals(type)){
//            根据编号查询book信息
            String id = req.getParameter("id");
            Book book = bookService.queryById(Integer.parseInt(id));
            req.setAttribute("book", book);
            req.getRequestDispatcher("/book/bookUpdate.jsp").forward(req, resp);
        } else if (Constant.SERVLET_TYPE_UPDATE.equals(type)) {
//            完成Book数据的添加，和save共用一个方法
            saveBook(req, resp);
        } else if (Constant.SERVLET_TYPE_DELETE.equals(type)) {
            deleteById(req, resp);
        } else {
//            啥都没做就是普通的查询操作
            queryBookList(req, resp);
        }
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //            逻辑删除book信息
        String id = req.getParameter("id");
        bookService.deleteById(Integer.parseInt(id));
        resp.sendRedirect("/bookServlet?type=query");
    }

    private void queryBookList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //                查询所有书籍信息
        String page = req.getParameter("no");
//        System.out.println(page);
        if(page == null || page.equals("")) {
            page="1";
        }

        PageBean<Book> list = bookService.queryAll(page);
//        List<Book> list = bookService.list(null);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/book/book.jsp").forward(req, resp);
    }





//    @DateTimeFormat(pattern = "yyyy-MM-dd")
//    Book book = new Book();
//    private Date pubdate = book.setPubdate(pubdate);
    //        添加书籍的方法
    private void saveBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Book book = new Book();
        String id = req.getParameter("id");
        book.setBookName(req.getParameter("bookName"));
        book.setAuthor(req.getParameter("author"));
        book.setPublish(req.getParameter("publish"));
        book.setIsbn(req.getParameter("isbn"));
        book.setLanguage(req.getParameter("language"));
        book.setPrice(req.getParameter("price"));

        String pubdate = req.getParameter("pubdate");
        book.setPubdate(req.getParameter("pubdate"));
        if (pubdate == null || "".equals(pubdate)){
            pubdate = "0000-00-00";
            book.setPubdate(pubdate);
        }

        book.setPressmark(req.getParameter("pressmark"));
        book.setIntroduction(req.getParameter("introduction"));
        Integer count = -1;
        if (id == null || id.equals("")){
//                count:影响的行数
            count = bookService.save(book);
        } else {
            book.setId(Integer.parseInt(id));
//            更新
            count = bookService.updateBook(book);
        }
        if (count > 0){
//                    表示增/更成功,再做一次查询操作
            resp.sendRedirect("/bookServlet?type=query");
        } else {
//                    插入失败
            System.out.println("插入失败...");
//                    跳转失败页面
        }
    }
}
